﻿using System;
using System.IO;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Data;
using System.Diagnostics;
using System.Drawing;

namespace MakeIT_Bill_Maker.Report
{
    class PdfReport
    {
        public void printPdf(DataTable dt, DataTable dt1, string m, string y)
        {
            #region Bills
            try
            {
                if (m.Equals("0"))
                {
                    y = (int.Parse(y) - 1).ToString();
                    m = "12";
                }

                Document oDoc = new Document(PageSize.A4, 30, 30, 30, 30);
                PdfWriter.GetInstance(oDoc, new FileStream("BILL_OF_"+y+"_"+m+".pdf", FileMode.Create));
                oDoc.Open();

                // Taking month in text
                string mt = "";
                switch (m)
                {
                    case "1": mt = "JANUARY"; break;
                    case "2": mt = "FEBRUARY"; break;
                    case "3": mt = "MARCH"; break;
                    case "4": mt = "APRIL"; break;
                    case "5": mt = "MAY"; break;
                    case "6": mt = "JUNE"; break;
                    case "7": mt = "JULY"; break;
                    case "8": mt = "AUGUST"; break;
                    case "9": mt = "SEPTEMBER"; break;
                    case "10": mt = "OCTOBER"; break;
                    case "11": mt = "NOVEMBER"; break;
                    case "12": mt = "DECEMBER"; break;
                }

                //for (int i = dt.Rows.Count - 1; i >= 0; i--)
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Phrase newline = new Phrase("\n");

                    // Start PDF header
                    Paragraph head1 = new Paragraph("EDMONTON APARTMENTS", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER, 16));
                    Paragraph head2 = new Paragraph("WATER BILL - "+mt+" "+y+ "\n", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER, 16, iTextSharp.text.Font.BOLD));
                    head1.Alignment = (Element.ALIGN_CENTER);
                    head2.Alignment = (Element.ALIGN_CENTER);
                    oDoc.Add(head1);
                    oDoc.Add(head2);
                    // End of PDF header
                    
                    oDoc.Add(newline);

                    float[] widths1 = new float[] { 40, 100 };
                    PdfPTable table1 = new PdfPTable(widths1);
                    
                    // APT. NO.
                    table1.DefaultCell.FixedHeight = 24;
                    table1.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                    table1.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.LightGray);
                    table1.AddCell("Apartment No.");
                    table1.DefaultCell.HorizontalAlignment = (Element.ALIGN_LEFT);
                    table1.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                    table1.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.White);
                    table1.AddCell("     " + dt.Rows[i]["AptNo"].ToString());

                    // NAME
                    table1.DefaultCell.FixedHeight = 24;
                    table1.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                    table1.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.LightGray);
                    table1.AddCell("Name");
                    table1.DefaultCell.HorizontalAlignment = (Element.ALIGN_LEFT);
                    table1.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                    table1.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.White);
                    table1.AddCell("     " + dt.Rows[i]["Name"].ToString());

                    // METER NO.
                    table1.DefaultCell.FixedHeight = 24;
                    table1.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                    table1.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.LightGray);
                    table1.AddCell("Water Meter No.");
                    table1.DefaultCell.HorizontalAlignment = (Element.ALIGN_LEFT);
                    table1.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                    table1.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.White);
                    table1.AddCell("     " + dt.Rows[i]["MeterNo"].ToString());

                    oDoc.Add(table1);
                    
                    oDoc.Add(newline);

                    PdfPTable table2 = new PdfPTable(2);
                    table2.TotalWidth = (900);

                    // CURRENT READING
                    table2.DefaultCell.FixedHeight = 24;
                    table2.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                    table2.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.LightGray);
                    table2.AddCell("Current   Reading on :   " + dt.Rows[i]["CurrReadDate"].ToString());
                    table2.DefaultCell.HorizontalAlignment = (Element.ALIGN_RIGHT);
                    table2.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                    table2.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.White);
                    table2.AddCell(dt.Rows[i]["CurrRead"].ToString());

                    // PREVIOUS READING 
                    table2.DefaultCell.FixedHeight = 24;
                    table2.DefaultCell.HorizontalAlignment = (Element.ALIGN_LEFT);
                    table2.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                    table2.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.LightGray);
                    table2.AddCell("Previous Reading on :   " + dt.Rows[i]["PrevReadDate"].ToString());
                    table2.DefaultCell.HorizontalAlignment = (Element.ALIGN_RIGHT);
                    table2.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                    table2.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.White);
                    table2.AddCell(dt.Rows[i]["PrevRead"].ToString());

                    // NO OF UNITS CONSUMED
                    table2.DefaultCell.FixedHeight = 24;
                    table2.DefaultCell.HorizontalAlignment = (Element.ALIGN_LEFT);
                    table2.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                    table2.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.LightGray);
                    table2.AddCell("No. of Units Consumed");
                    table2.DefaultCell.HorizontalAlignment = (Element.ALIGN_RIGHT);
                    table2.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                    table2.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.White);
                    table2.AddCell(dt.Rows[i]["UnitsConsumed"].ToString());
                    
                    // CHARGE FOR WATER CONSUMPTION 
                    table2.DefaultCell.FixedHeight = 24;
                    table2.DefaultCell.HorizontalAlignment = (Element.ALIGN_LEFT);
                    table2.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                    table2.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.LightGray);
                    table2.AddCell("Charge (as per Category 18 - NWSDB)");
                    table2.DefaultCell.HorizontalAlignment = (Element.ALIGN_RIGHT);
                    table2.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                    table2.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.White);
                    table2.AddCell(dt.Rows[i]["Charge"].ToString() + ".00");


                    // SERVICE CHARGE 
                    table2.DefaultCell.FixedHeight = 24;
                    table2.DefaultCell.HorizontalAlignment = (Element.ALIGN_LEFT);
                    table2.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                    table2.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.LightGray);
                    table2.AddCell("Service Charge");
                    table2.DefaultCell.HorizontalAlignment = (Element.ALIGN_RIGHT);
                    table2.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                    table2.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.White);

                    table2.AddCell(dt.Rows[i]["Service"].ToString() + ".00");
                    
                    // TOTAL CHARGE FOR THIS MONTH
                    table2.DefaultCell.FixedHeight = 24;
                    table2.DefaultCell.HorizontalAlignment = (Element.ALIGN_LEFT);
                    table2.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                    table2.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.LightGray);
                    table2.AddCell("CHARGES FOR MONTH");
                    table2.DefaultCell.HorizontalAlignment = (Element.ALIGN_RIGHT);
                    table2.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                    table2.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.White);
                    table2.AddCell(dt.Rows[i]["Total"].ToString() + ".00");

                    // ARREARS OUTSTANDING
                    table2.DefaultCell.FixedHeight = 24;
                    table2.DefaultCell.HorizontalAlignment = (Element.ALIGN_LEFT);
                    table2.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                    table2.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.LightGray);
                    table2.AddCell("ARREARS");
                    table2.DefaultCell.HorizontalAlignment = (Element.ALIGN_RIGHT);
                    table2.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                    table2.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.White);
                    table2.AddCell(dt.Rows[i]["m.Arrears"].ToString() + ".00");

                    // PENALTY
                    table2.DefaultCell.FixedHeight = 24;
                    table2.DefaultCell.HorizontalAlignment = (Element.ALIGN_LEFT);
                    table2.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                    table2.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.LightGray);
                    table2.AddCell("PENALTY");
                    table2.DefaultCell.HorizontalAlignment = (Element.ALIGN_RIGHT);
                    table2.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                    table2.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.White);
                    table2.AddCell(dt.Rows[i]["Penalty"].ToString() + ".00");

                    // TOTAL AMOUNT DUE
                    table2.DefaultCell.FixedHeight = 24;
                    table2.DefaultCell.HorizontalAlignment = (Element.ALIGN_LEFT);
                    table2.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                    table2.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.LightGray);
                    table2.AddCell("TOTAL PAYABLE");
                    table2.DefaultCell.HorizontalAlignment = (Element.ALIGN_RIGHT);
                    table2.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                    table2.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.White);
                    table2.AddCell(dt.Rows[i]["TotalAmount"].ToString() + ".00");
                    
                    oDoc.Add(table2);

                    oDoc.Add(newline);

                    // Details of Arrears
                    Paragraph head8 = new Paragraph("Details of arrears : ...............................                    ...............................\n                               ...............................                    ...............................\n                               ...............................                    ...............................");
                    head8.Alignment = (Element.ALIGN_CENTER);
                    oDoc.Add(head8);

                    // Maintenance fees due
                    Paragraph head11 = new Paragraph("\nMAINTENANCE FEES DUE : ...............................  (..............-..............)");
                    head11.Alignment = (Element.ALIGN_CENTER);
                    oDoc.Add(head11);

                    oDoc.Add(newline);

                    Paragraph head3 = new Paragraph("Final date of payment : " + dt.Rows[i]["FinalDateOfPayment"].ToString(), new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER, 14, iTextSharp.text.Font.BOLD));
                    head3.Alignment = (Element.ALIGN_CENTER);
                    oDoc.Add(head3);

                    oDoc.Add(newline);

                    Paragraph head4 = new Paragraph("Date : .................." + "              ..................\n                                        Hony. Treasurer", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER, 12, iTextSharp.text.Font.BOLD));
                    head4.Alignment = (Element.ALIGN_CENTER);
                    oDoc.Add(head4);

                    Paragraph head9 = new Paragraph("Notes:", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER, 10, iTextSharp.text.Font.BOLD));
                    head9.Alignment = (Element.ALIGN_LEFT);
                    oDoc.Add(head9);

                    Paragraph head5 = new Paragraph("1. If the amount shown as arrears does not tally with the records of the Block Treasurers, copies       of receipts etc. should be presented.", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER, 9, iTextSharp.text.Font.BOLD));
                    head5.Alignment = (Element.ALIGN_JUSTIFIED);
                    oDoc.Add(head5);

                    Paragraph head6 = new Paragraph("2. Residents whose water meters are out of order will be charged the average monthly usage for 3        previous months, when the meter was in order.", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER, 9, iTextSharp.text.Font.BOLD));
                    head6.Alignment = (Element.ALIGN_LEFT);
                    oDoc.Add(head6);

                    Paragraph head7 = new Paragraph("3. Please do not pay to the Committee Account at People's Bank as it creates problems in                maintaining accounts.", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER, 9, iTextSharp.text.Font.BOLD));
                    head7.Alignment = (Element.ALIGN_LEFT);
                    oDoc.Add(head7);

                    Paragraph head10 = new Paragraph("4. Monthly Maintenance fees and arrears if any, should be paid together with the water bill. If you     need any clarifications, please speak to your Block Treasurer or the Management Committee            Treasurer at " + dt1.Rows[0]["TAddress"].ToString() + ", Tel: " + dt1.Rows[0]["TTel"].ToString() + ".", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER, 9, iTextSharp.text.Font.BOLD));
                    head10.Alignment = (Element.ALIGN_LEFT);
                    oDoc.Add(head10);

                    Paragraph head12 = new Paragraph("5. Payments can be made to the Executive Committee Treasurer from " + dt1.Rows[0]["TimeFrom"].ToString() + " to " + dt1.Rows[0]["TimeTo"].ToString() + ",              " + dt1.Rows[0]["Days"].ToString(), new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER, 9, iTextSharp.text.Font.BOLD));
                    head10.Alignment = (Element.ALIGN_LEFT);
                    oDoc.Add(head12);

                    oDoc.NewPage();
                }

                oDoc.Close();
                MessageBox.Show("Report is Succesfully Generated.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Process.Start(Application.StartupPath + "/BILL_OF_" + y + "_" + m + ".pdf");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            #endregion

            #region Summary
            // Summary Document
            try
            {
                if (m.Equals("0"))
                {
                    y = (int.Parse(y) - 1).ToString();
                    m = "12";
                }

                Document oDoc1 = new Document(PageSize.A4, 30, 30, 30, 30);
                PdfWriter.GetInstance(oDoc1, new FileStream("SUMMARY_OF_" + y + "_" + m + ".pdf", FileMode.Create));
                oDoc1.Open();

                // Taking month in text
                string mt = "";
                switch (m)
                {
                    case "1": mt = "JANUARY"; break;
                    case "2": mt = "FEBRUARY"; break;
                    case "3": mt = "MARCH"; break;
                    case "4": mt = "APRIL"; break;
                    case "5": mt = "MAY"; break;
                    case "6": mt = "JUNE"; break;
                    case "7": mt = "JULY"; break;
                    case "8": mt = "AUGUST"; break;
                    case "9": mt = "SEPTEMBER"; break;
                    case "10": mt = "OCTOBER"; break;
                    case "11": mt = "NOVEMBER"; break;
                    case "12": mt = "DECEMBER"; break;
                }

                int totArrears = 0, totTotal = 0, totTotalAmount = 0 ;

                //string blockDefault = dt.Rows[dt.Rows.Count - 1]["AptNo"].ToString().Substring(0, 3);
                string blockDefault = dt.Rows[0]["AptNo"].ToString().Substring(0, 3);
                
                //string block = dt.Rows[dt.Rows.Count - 1]["AptNo"].ToString().Substring(0, 3);
                string block = dt.Rows[0]["AptNo"].ToString().Substring(0, 3);
                float[] widths = new float[] { 35, 100, 35, 35, 35 };

                oDoc1.NewPage();

                Phrase newline = new Phrase("\n");

                // Start PDF header
                Paragraph head1 = new Paragraph("EDMONTON APARTMENTS - BLOCK " + block, new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER, 16));
                Paragraph head2 = new Paragraph("WATER USE SUMMARY - " + mt + " " + y + "\n", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER, 16, iTextSharp.text.Font.BOLD));
                head1.Alignment = (Element.ALIGN_CENTER);
                head2.Alignment = (Element.ALIGN_CENTER);
                oDoc1.Add(head1);
                oDoc1.Add(head2);
                // End of PDF header

                oDoc1.Add(newline);

                // Table Header
                PdfPTable table5 = new PdfPTable(widths);
                
                table5.DefaultCell.FixedHeight = 25;
                table5.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                table5.DefaultCell.HorizontalAlignment = (Element.TITLE);
                table5.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.LightGray);
                table5.AddCell("Apt.No.");
                table5.AddCell("Name");
                table5.AddCell("Arrears");
                table5.AddCell("Charges");
                table5.AddCell("Total");
                // End of Table Header

                oDoc1.Add(table5);

                //for (int i = dt.Rows.Count-1; i >=0 ; i--)
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string blockTemp = dt.Rows[i]["AptNo"].ToString().Substring(0, 4);
                    if (blockTemp.Equals("XB10"))
                        block = blockTemp;
                    else
                        block = dt.Rows[i]["AptNo"].ToString().Substring(0, 3);

                    // New page
                    if (!block.Equals(blockDefault))
                    {
                        // Table Header
                        PdfPTable table6 = new PdfPTable(widths);

                        table6.DefaultCell.FixedHeight = 25;
                        table6.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                        table6.DefaultCell.HorizontalAlignment = (Element.TITLE);
                        table6.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.LightGray);
                        table6.AddCell("Total");
                        table6.AddCell("");
                        table6.DefaultCell.HorizontalAlignment = (Element.ALIGN_RIGHT);
                        table6.AddCell(totArrears.ToString() + ".00");
                        table6.AddCell(totTotal.ToString() + ".00");
                        table6.AddCell(totTotalAmount.ToString() + ".00");
                        // End of Table Header

                        oDoc1.Add(table6);

                        blockDefault = block;
                        oDoc1.NewPage();

                        // Start PDF header
                        head1 = new Paragraph("EDMONTON APARTMENTS - BLOCK " + block, new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER, 16));
                        head2 = new Paragraph("WATER USE SUMMARY - " + mt + " " + y + "\n", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER, 16, iTextSharp.text.Font.BOLD));
                        head1.Alignment = (Element.ALIGN_CENTER);
                        head2.Alignment = (Element.ALIGN_CENTER);
                        oDoc1.Add(head1);
                        oDoc1.Add(head2);
                        // End of PDF header

                        oDoc1.Add(newline);

                        // Table Header
                        PdfPTable table3 = new PdfPTable(widths);

                        table3.DefaultCell.FixedHeight = 25;
                        table3.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                        table3.DefaultCell.HorizontalAlignment = (Element.TITLE);
                        table3.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.LightGray);
                        table3.AddCell("Apt.No.");
                        table3.AddCell("Name");
                        table3.AddCell("Arrears");
                        table3.AddCell("Charges");
                        table3.AddCell("Total");
                        // End of Table Header

                        oDoc1.Add(table3);

                        totArrears = 0;
                        totTotal = 0;
                        totTotalAmount = 0;
                    }

                    PdfPTable table4 = new PdfPTable(widths);
                    table4.TotalWidth = (900);

                    // RECORD
                    table4.DefaultCell.FixedHeight = 25;
                    table4.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                    table4.DefaultCell.HorizontalAlignment = (Element.ALIGN_LEFT);
                    table4.AddCell(dt.Rows[i]["AptNo"].ToString());
                    table4.AddCell(dt.Rows[i]["Name"].ToString());
                    table4.DefaultCell.HorizontalAlignment = (Element.ALIGN_RIGHT);
                    table4.AddCell(dt.Rows[i]["m.Arrears"].ToString() + ".00");
                    table4.AddCell(dt.Rows[i]["Total"].ToString() + ".00");
                    table4.AddCell(dt.Rows[i]["TotalAmount"].ToString() + ".00");

                    oDoc1.Add(table4);

                    totArrears += int.Parse(dt.Rows[i]["m.Arrears"].ToString());
                    totTotal += int.Parse(dt.Rows[i]["Total"].ToString());
                    totTotalAmount += int.Parse(dt.Rows[i]["TotalAmount"].ToString());
                }

                // Table Header
                PdfPTable table7 = new PdfPTable(widths);

                table7.DefaultCell.FixedHeight = 25;
                table7.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                table7.DefaultCell.HorizontalAlignment = (Element.TITLE);
                table7.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.LightGray);
                table7.AddCell("Total");
                table7.AddCell("");
                table7.DefaultCell.HorizontalAlignment = (Element.ALIGN_RIGHT);
                table7.AddCell(totArrears.ToString() + ".00");
                table7.AddCell(totTotal.ToString() + ".00");
                table7.AddCell(totTotalAmount.ToString() + ".00");
                // End of Table Header

                oDoc1.Add(table7);

                oDoc1.Close();
                MessageBox.Show("Summary is Succesfully Generated.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Process.Start(Application.StartupPath + "/SUMMARY_OF_" + y + "_" + m + ".pdf");
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            #endregion

            #region Records
            // Summary Document
            try
            {
                if (m.Equals("0"))
                {
                    y = (int.Parse(y) - 1).ToString();
                    m = "12";
                }

                m = (int.Parse(m) + 1).ToString();
                Document oDoc1 = new Document(PageSize.A4, 30, 30, 30, 30);
                PdfWriter.GetInstance(oDoc1, new FileStream("READINGS_OF_" + y + "_" + m + ".pdf", FileMode.Create));
                oDoc1.Open();

                // Taking month in text
                string mt = "";
                switch (m)
                {
                    case "1": mt = "JANUARY"; break;
                    case "2": mt = "FEBRUARY"; break;
                    case "3": mt = "MARCH"; break;
                    case "4": mt = "APRIL"; break;
                    case "5": mt = "MAY"; break;
                    case "6": mt = "JUNE"; break;
                    case "7": mt = "JULY"; break;
                    case "8": mt = "AUGUST"; break;
                    case "9": mt = "SEPTEMBER"; break;
                    case "10": mt = "OCTOBER"; break;
                    case "11": mt = "NOVEMBER"; break;
                    case "12": mt = "DECEMBER"; break;
                }

                //string blockDefault = dt.Rows[dt.Rows.Count - 1]["AptNo"].ToString().Substring(0, 3);
                string blockDefault = dt.Rows[0]["AptNo"].ToString().Substring(0, 3);

                //string block = dt.Rows[dt.Rows.Count - 1]["AptNo"].ToString().Substring(0, 3);
                string block = dt.Rows[0]["AptNo"].ToString().Substring(0, 3);
                float[] widths = new float[] { 30, 100, 35, 35 };

                oDoc1.NewPage();

                Phrase newline = new Phrase("\n");

                // Start PDF header
                Paragraph head1 = new Paragraph("EDMONTON APARTMENTS - BLOCK " + block, new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER, 16));
                Paragraph head2 = new Paragraph("WATER METER READINGS - " + mt + " " + y + "\n", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER, 16, iTextSharp.text.Font.BOLD));
                head1.Alignment = (Element.ALIGN_CENTER);
                head2.Alignment = (Element.ALIGN_CENTER);
                oDoc1.Add(head1);
                oDoc1.Add(head2);
                // End of PDF header

                oDoc1.Add(newline);

                // Table Header
                PdfPTable table5 = new PdfPTable(widths);

                table5.DefaultCell.FixedHeight = 25;
                table5.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                table5.DefaultCell.HorizontalAlignment = (Element.TITLE);
                table5.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.LightGray);
                table5.AddCell("Apt.No.");
                table5.AddCell("Name");
                table5.AddCell("Previous");
                table5.AddCell("Current");
                // End of Table Header

                oDoc1.Add(table5);

                //for (int i = dt.Rows.Count-1; i >=0 ; i--)
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string blockTemp = dt.Rows[i]["AptNo"].ToString().Substring(0, 4);
                    if (blockTemp.Equals("XB10"))
                        block = blockTemp;
                    else
                        block = dt.Rows[i]["AptNo"].ToString().Substring(0, 3);

                    // New page
                    if (!block.Equals(blockDefault))
                    {
                        blockDefault = block;
                        oDoc1.NewPage();

                        // Start PDF header
                        head1 = new Paragraph("EDMONTON APARTMENTS - BLOCK " + block, new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER, 16));
                        head2 = new Paragraph("WATER METER READINGS - " + mt + " " + y + "\n", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER, 16, iTextSharp.text.Font.BOLD));
                        head1.Alignment = (Element.ALIGN_CENTER);
                        head2.Alignment = (Element.ALIGN_CENTER);
                        oDoc1.Add(head1);
                        oDoc1.Add(head2);
                        // End of PDF header

                        oDoc1.Add(newline);

                        // Table Header
                        PdfPTable table3 = new PdfPTable(widths);

                        table3.DefaultCell.FixedHeight = 25;
                        table3.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                        table3.DefaultCell.HorizontalAlignment = (Element.TITLE);
                        table3.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.LightGray);
                        table3.AddCell("Apt.No.");
                        table3.AddCell("Name");
                        table3.AddCell("Previous");
                        table3.AddCell("Current");
                        // End of Table Header

                        oDoc1.Add(table3);
                    }

                    PdfPTable table4 = new PdfPTable(widths);
                    table4.TotalWidth = (900);

                    // RECORD
                    table4.DefaultCell.FixedHeight = 25;
                    table4.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                    table4.DefaultCell.HorizontalAlignment = (Element.ALIGN_LEFT);
                    table4.AddCell(dt.Rows[i]["AptNo"].ToString());
                    table4.AddCell(dt.Rows[i]["Name"].ToString());
                    table4.DefaultCell.HorizontalAlignment = (Element.ALIGN_RIGHT);
                    table4.AddCell(dt.Rows[i]["CurrRead"].ToString());
                    table4.AddCell("");

                    oDoc1.Add(table4);
                }

                oDoc1.Close();
                MessageBox.Show("'Readings' Document is Succesfully Generated.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Process.Start(Application.StartupPath + "/READINGS_OF_" + y + "_" + m + ".pdf");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            #endregion
        }

        public void printPdf()
        {
            frmReport f = new frmReport();
            f.Show();
        }
    }
}
